﻿// -----------------------------------------------------------------------
// <copyright file="HistoryWriter.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ClearLines.Stocks.Domain
{
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using Microsoft.Office.Interop.Excel;

   /// <summary>
   /// TODO: Update summary.
   /// </summary>
   public class HistoryWriter
   {
      public const int DateColumn = 0;
      public const int OpenColumn = 1;
      public const int CloseColumn = 2;

      public static void Write(StockHistory history, Worksheet worksheet)
      {
         var length = history.History.Count();

         var dataArray = new object[length + 1, 6];
         var formatArray = new object[length + 1,6];

         dataArray[0, DateColumn] = "Date";
         dataArray[0, OpenColumn] = "Open";
         dataArray[0, CloseColumn] = "Close";

         var line = 1;
         foreach (var day in history.History)
         {
            dataArray[line, DateColumn] =  day.Day;
            dataArray[line, OpenColumn] = day.Open;
            dataArray[line, CloseColumn] = day.Close;

            formatArray[line, DateColumn] = "YYYY/MM/DD";
            formatArray[line, OpenColumn] = "#,##0.00";
            formatArray[line, CloseColumn] = "#,##0.00";

            line++;
         }

         var upperLeftCell = worksheet.Cells[1, 1];
         var lowerRightCell = worksheet.Cells[length + 1, 6];

         var range = worksheet.Range[upperLeftCell, lowerRightCell];
         range.Value2 = dataArray;
         //range.NumberFormat = formatArray;
      }
   }
}
